package com.liph.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author lipenghui
 * @date 2022-12-06
 * @description 设置用户名密码方式二：基于UserDetailsService接口设置用户名密码
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    /***
     * @author lipenghui
     * @date 2022/12/6
     * @param
     * @description 注入密码加密解码器
     **/
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        // TODO  自定义登录逻辑 查MySQL



        // 设置密码加密的方式一：
        // 指定一种密码加密方式
        String pwd = passwordEncoder.encode("123456");
        // 通过User构建UserDetails对象
        UserDetails userDetails = new User("fox3", pwd, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));

        // 设置密码加密的方式二：
//        UserDetails userDetails = User.withUsername("fox2")
//                .password("{noop}123456").authorities("admin").build();
        // "{noop}123456" 表示密码不加密

        return userDetails;
    }
}

